if (!window.WebSocket)
	alert("WebSocket not supported by this browser");

var game = {
		join: function() {
			var location = document.location.toString().replace('http://','ws://').replace('https://','wss://') + "Engine";
			this._ws=new WebSocket(location);
			this._ws.onopen=this._onopen;
			this._ws.onmessage=this._onmessage;
			this._ws.onclose=this._onclose;
		},
		rejoin: function(){
			this._close(0);
			var location = document.location.toString().replace('http://','ws://').replace('https://','wss://') + "Engine";
			this._ws=new WebSocket(location);
			//this._ws.onopen=this._onopen;
			this._ws.onmessage=this._onmessage;
		},

		_onopen: function(){
			game._send("isGameRunning");
		},

		_send: function(message){
			if (this._ws){
				this._ws.send(message);
			}
			else {
				alert("connection closed");
			}
		},

		chat: function(text) {
			if (text != null && text.length>0 )
				game._send(game._username,text);
		},

		_onmessage: function(m) {
			console.log(m.data.toString());
			if (m.data){
				eval(m.data.toString());
				if (m.data.toString() == "running = false" || m.data.toString() == "running = true" ){
					startGame(running);
				}
				
			}
		},

		_close: function(m) {
			this._ws=null;
			alert("connection closed.")
		}

};
